package com.bw.kf.all2211a.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bw.kf.mvvm.base.BaseRepo
import com.bw.kf.mvvm.base.LoginMEntity
import com.bw.kf.mvvm.entity.LoginEntity
import com.bw.kf.mvvm.entity.SendCodeEntity
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.util.Objects

class LoginRepo : BaseRepo() {
 val success=MutableLiveData<SendCodeEntity>()
 val fall=MutableLiveData<String>()
     fun sendCode(phone:String){
     viewModelScope.launch {
         try {
             success.value=aPiText.sendCode(phone)
         }catch(e:Exception) {
             fall.value=e.message
         }
     }
    }
    val successL=MutableLiveData<LoginEntity>()
    val fallL=MutableLiveData<String>()
    fun login(phone:String,code:String){
          viewModelScope.launch {
              try {
               val map= HashMap<String,String>()
               map.put("captcha",code)
               map.put("phone",phone)
                  val gson= Gson().toJson(map)
                  val body=  RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(),gson)
                  successL.value=aPiText.login(body)
              } catch (e:Exception){
                  fallL.value=e.message
              }
          }
    }
    val successM= MutableLiveData<LoginMEntity>()
    val fallM= MutableLiveData<String>()
    fun Mlogin(phone:String,password:String){
        viewModelScope.launch {
            try {
                val map= mutableMapOf<String,String>()
                map.put("account",phone)
                map.put("password",password)
                val gson=Gson().toJson(map)
                val body=RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(),gson)
                successM.value=aPiText.Mlogin(body)
            }catch (e:Exception) {
                fallM.value=e.message
            }
        }
    }
}